


//
//  ZStoreDetailViewController.m
//  ZQLearn
//
//  Created by macfeel on 2019/4/23.
//  Copyright © 2019 macfeel. All rights reserved.
#import "ZPayViewController.h"
#import "ZCartViewController.h"
#import "CartAndBuyView.h"
#import "ZStoreDetailViewController.h"
#import "CommodityDetailHeadView.h"
#import <WebKit/WebKit.h>
#import "SDCycleScrollView.h"
@interface ZStoreDetailViewController ()<WKUIDelegate,WKNavigationDelegate,UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)WKWebView *webView;
@property (nonatomic,strong)CommodityDetailHeadView *head;

@property (nonatomic,strong)SDCycleScrollView *cycleView;

@property(nonatomic,copy)NSString *price;


#define cellIdentifier @"cel"
#define cellIHeight 900
@end

@implementation ZStoreDetailViewController
-(UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight-64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = cellIHeight;
        _tableView.backgroundColor = kBackgroundColor;
        //        [_tableView registerNib:[UINib nibWithNibName:@"NewsCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
        //        WeakStament(ws);
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setItemWithTitle:@"商品详情" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    
    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 40, 40) title:@"" Image:@"gouwuche" selectedIMG:@"" tartget:self action:@selector(gotoCart)];
    
    CommodityDetailHeadView *head = [[[NSBundle mainBundle]loadNibNamed:@"CommodityDetailHeadView" owner:self options:nil]lastObject];
    self.head = head;
//    [self addWebView];
    self.tableView.tableHeaderView = head;
//    self.tableView.tableFooterView = self.webView;
    [self.view addSubview:self.tableView];
    
    [self addBannerView];
    
    ///购买按钮
    [self addBottomBuyView];
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    if (self.model) {
        
        self.head.titleLab.text = self.model.name;
        self.head.scoreLab.text = [NSString stringWithFormat:@"%@积分",self.model.integral];
        self.head.priceLab.text = [NSString stringWithFormat:@"市场参考价:%.1f元",[self.model.commodityPrice floatValue]];
        
        NSString *sss = [NSString stringWithFormat:@"%.0f",[self.model.commodityPrice floatValue]];
        self.price = sss;
        YYLog(@"%@",self.price);
        
        self.head.guigecanshuLab.text = [NSString stringWithFormat:@"规格参数:%@",self.model.specification];
        
    }
}
#pragma mark - 轮播图视图
-(void)addBannerView{
    _cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, ScreenWidth, 280) delegate:self placeholderImage:nil];
    _cycleView.currentPageDotColor = kMagentaColor;
    _cycleView.pageDotColor = kBackgroundColor;
    ///本地图片
    _cycleView.localizationImageNamesGroup = self.urlArr;
    _cycleView.autoScrollTimeInterval = 4;
    [self.tableView addSubview:self.cycleView];
}

#define BarHeight 45.f

///添加底部购买bar
- (void)addBottomBuyView{

    
    UIButton *cartBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cartBtn.frame = CGRectMake(0, ScreenHeight-BarHeight-64, ScreenWidth/2, BarHeight);
    cartBtn.backgroundColor = [UIColor orangeColor];
    [cartBtn addTarget:self action:@selector(cartAction:) forControlEvents:UIControlEventTouchUpInside];
    [cartBtn setTitle:@"加入购物车" forState:UIControlStateNormal];
    [self.view addSubview:cartBtn];
    
    UIButton *gouBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    gouBtn.frame = CGRectMake(ScreenWidth/2, ScreenHeight-BarHeight-64, ScreenWidth/2, BarHeight);
    gouBtn.backgroundColor = [UIColor redColor];
    [gouBtn addTarget:self action:@selector(gouAction:) forControlEvents:UIControlEventTouchUpInside];
    [gouBtn setTitle:@"立即购买" forState:UIControlStateNormal];
    [self.view addSubview:gouBtn];

}

- (void)cartAction:(UIButton *)btn
{
    YYLog(@"加购");
    ///加购接口
    NSDictionary *dict = @{@"commodityInfo.id":self.cid?:@"",@"userInfo.id":[UserInfoSave shareInstance].uid?:@"",@"commodityNumber":@(1),};
    [ZNetworkService postRequestWithBaseUrl:kAddToCartUrl setParameters:dict success:^(id result) {
        YYLog(@"%@",result);
        if ([result[@"errorCode"]integerValue]==0) {
            [AFToast showText: @"添加成功"];
        }else{
             [AFToast showText: @"添加失败"];
        }
    } fail:^(NSError *error) {
        
    } delegater:nil];
}

#pragma mark - 点击购买
- (void)gouAction:(UIButton *)btn
{
    ZPayViewController *pay = [ZPayViewController new];
    pay.flag = @"cdetail";
    pay.strAmount = self.price?:@"";
    pay.storeModel = self.model;
    [self.navigationController pushViewController:pay animated:YES];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    
    WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 20, ScreenWidth, cellIHeight)];
    
    ///webview 参数链接  url参数
    NSString *urlString = [NSString stringWithFormat:@"http://sdxx.bestzhiqinweike.com/app/commodity/findCommodityInfoDetail?id=%@",self.cid];
    YYLog(@"%@",urlString);
    [webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
    webView.UIDelegate = self;
    webView.navigationDelegate = self;
    [cell.contentView addSubview:webView];
    return cell;
}

////
- (void)addWebView
{
    
    WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 436, ScreenWidth, ScreenHeight)];
    
    ///webview 参数链接
    NSString *urlString = [NSString stringWithFormat:@"http://sdxx.bestzhiqinweike.com/app/commodity/findCommodityInfoDetail?id=%@",self.cid];
    YYLog(@"%@",urlString);
    [webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
    webView.UIDelegate = self;
    webView.navigationDelegate = self;
    self.webView = webView;
    
}


#pragma mark - 跳转到购物车
- (void)gotoCart{
    ZCartViewController *cart = [ZCartViewController new];
    [self.navigationController pushViewController:cart animated:YES];
}

@end
